package org.esk.dablog.web.functions;

import org.esk.dablog.model.entries.Ride;
import org.esk.dablog.model.Author;
import org.esk.dablog.model.Entry;
import org.esk.dablog.model.Picture;
import org.esk.dablog.ApplicationConstants;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.StringEscapeUtils;

/**
 * This class provides JSP2.0 functions related to Entry entity
 */
public class EntryFunctions {

    /**
     * checks if author is on the ride
     * @param r
     * @param a
     * @return
     */
    public static boolean isAuthorOnRide(Ride r, Author a) {
        if (a == null || r == null) {
            return false;
        }
        return r.isOnRide(a.getUsername());
    }

    /**
     * checks if user is on the ride
     * @param r
     * @param username
     * @return
     */
    public static boolean isUserOnRide(Ride r, String username) {
        if (username == null || r == null) {
            return false;
        }
        return r.isOnRide(username);
    }

    /**
     * checks if user is allowed to edit the entry - i.e. checks is
     * entry is written by the menstioned user
     *
     * @param e
     * @param username
     * @return
     */
    public static boolean isAllowedEditing(Entry e, String username) {
        if (e == null) {
            return false;
        }        
        return e.getAuthor().getUsername().equals(username);
    }

    /**
     * checks if user is allowed to edit the entry - i.e. checks is
     * entry is written by the menstioned user
     *
     * @param p
     * @param username
     * @return
     */
    public static boolean isPictureOwner(Picture p, String username) {
        if (p == null) {
            return false;
        }
        return p.getAuthor().getUsername().equals(username);
    }

    /**
     * Shows full stack trace for exception
     * @param ex
     * @return
     */
    public static String getFullStackTrace(Exception ex) {
        return ExceptionUtils.getFullStackTrace(ex);
    }

    /**
     * function replaces all '\n' occurences witn <br/> tag
     * and escapes other html and js things
     * @return
     */
    public static String toBr(String str) {
        if (str == null) {
            return null;
        }
        str = StringEscapeUtils.escapeHtml(str);
        return str.replace("\n", "<br/>");
    }

    /**
     * function replaces all '<br/>' occurences witn '\n' symbol
     * @return
     */
    public static String toCr(String str) {
        if (str == null) {
            return null;
        }
        str = StringEscapeUtils.unescapeHtml(str);
        return str.replace("<br/>", "\n");
    }


    /**
     * this function cuts entered text to given length
     * @param str
     * @return
     */
    public static String preview(String str) {
        return previewLength(str, ApplicationConstants.getInstance().getPreviewLength());
    }

    /**
     * checks if string needs to be shortened
     * @param str
     * @return
     */
    public static boolean isShortened(String str) {
        return isShortenedLength(str, ApplicationConstants.getInstance().getPreviewLength());
    }


    /**
     * this function cuts entered text to given length
     * @param str
     * @return
     */
    public static String previewComment(String str) {
        return previewLength(str, ApplicationConstants.getInstance().getCommentLength());
    }

    /**
     * checks if string needs to be shortened
     * @param str
     * @return
     */
    public static boolean isShortenedComment(String str) {
        return isShortenedLength(str, ApplicationConstants.getInstance().getCommentLength());
    }



    /**
     * this function cuts entered text to given length
     * @param str
     * @return
     */
    public static String previewLength(String str, int length) {
        if (str == null) {
            return null;
        }
        int pos = 0;
        boolean append = false;
        pos = StringUtils.indexOf(str, '.', length);
        if (pos < 0) {
            pos = StringUtils.indexOf(str, ';', length);
        }
        if (pos < 0) {
            pos = StringUtils.indexOf(str, ' ', length);
            append = true;
        }
        if (pos < 0) {
            pos = length;
            append = true;
        }
        if (str.length() > pos) {
            return append ? str.substring(0, pos) + "..." : str.substring(0, pos) + ".";

        }
        return str;
    }

    /**
     * checks if string needs to be shortened
     * @param str
     * @return
     */
    public static boolean isShortenedLength(String str, int length) {
        return str == null ? false : str.length() > length;
    }


    /**
     * Makes email not to be searchable by robots
     * @return
     * @param mail in mail
     */
    public static String mangleEmail(String mail) {
        String[] parts = StringUtils.split(mail, "@");
        if(parts == null || parts.length < 2) {
            return mail;
        }
        return " <script language='javascript'>document.write('" + parts[0] + "'); " +
                " document.write('@'); document.write('" + parts[1] + "'); " +
                " </script> <noscript> " + parts[0] + " .AT. " + parts[1] + "</noscript>";                                
    }
}
